package com.badu.app.service.impl;

import com.badu.app.domain.AppBanner;
import com.badu.app.dao.AppBannerMapper;
import com.badu.app.service.IAppBannerService;
import com.badu.common.utils.StringUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder123
 * @since 2021-01-14
 */
@Service
public class AppBannerServiceImpl extends ServiceImpl<AppBannerMapper, AppBanner> implements IAppBannerService {

	@Autowired
	private AppBannerMapper appBannerMapper;
	
	@Override
	public List<AppBanner> queryAppBanner() {
		List<AppBanner> selectList = appBannerMapper.selectList(new EntityWrapper<AppBanner>().eq("bannerIsShow", "0").orderBy(" bannerSort asc "));
		if(CollectionUtils.isEmpty(selectList))
			Assert.state(false, "没有轮播数据");
		
		return selectList.parallelStream().filter(f->{
				if(StringUtils.isNotBlank(f.getBannerShowType())) {
					//时间段显示
					if(StringUtils.equals(f.getBannerShowType(), "1")) {
						Date date = new Date("yyyy-MM-dd");
						Date bannerShowStartTime = f.getBannerShowStartTime();
						Date bannerShowEndTime = f.getBannerShowEndTime();
						if(bannerShowStartTime.before(date) && bannerShowEndTime.after(date)) {
							return true;
						}
					}
				}
				return false;
			}).collect(Collectors.toList());
	}

}
